package com.yiquan.comment.controller;

import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.yiquan.comment.pojo.Comment;
import com.yiquan.comment.service.CommentService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;

/**
 * 控制器层
 *
 * @author Administrator
 */
@RestController
@CrossOrigin
@RequestMapping("/comment")
@Slf4j
public class CommentController {

    @Autowired
    private CommentService commentService;


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", commentService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable Long id) {
        return new Result(true, StatusCode.OK, "查询成功", commentService.findById(id));
    }


    /**
     * 分页+多条件查询(用户登录状态下的)
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size, @RequestAttribute(value = "user_id", required = false) Long userId) {
        PageResult<Comment> search = commentService.findSearchLogin(searchMap, page, size, userId);
        return new Result(true, StatusCode.OK, "查询成功", search);
    }

    /**
     * 分页+多条件查询(用户未登录状态下的)
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/searchLogout/{page}/{size}", method = RequestMethod.POST)
    public Result findSearchLogout(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        log.info("根据条件查询，条件Map：{}", searchMap);
        PageResult<Comment> search = commentService.findSearch(searchMap, page, size);
        return new Result(true, StatusCode.OK, "查询成功", search);
    }

    /**
     * 后台分页搜索
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/backend/search/{page}/{size}")
    public Result searchCommentByBackend(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        log.info("根据条件查询，条件Map：{}", searchMap);
        PageResult<Comment> search = commentService.backendSearch(searchMap, page, size);
        return new Result(true, StatusCode.OK, "查询成功", search);
    }

    /**
     * 后台根据CommentId查询
     *
     * @param commentId
     * @return
     */
    @GetMapping("/backend/getCommentByCommentId")
    public Result findCommentByCommentIdBackend(@RequestParam Long commentId) {
        log.info("后台根据commentId查找：{}", commentId);
        return new Result(true, StatusCode.OK, "查询成功", commentService.findById(commentId));
    }


    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        log.info("根据条件查询，条件Map：{}", searchMap);
        return new Result(true, StatusCode.OK, "查询成功", commentService.findSearch(searchMap));
    }

    /**
     * 增加
     *
     * @param comment
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Comment comment, @RequestAttribute(value = "user_id", required = false) String user_id, @RequestParam Long userId) {
        log.info("收到请求，comment:{},userId:{}", comment, user_id);
        if (!StringUtils.isEmpty(user_id)) {
            commentService.add(comment, Long.valueOf(user_id));
        } else {
            commentService.add(comment, userId);
        }

        return new Result(true, StatusCode.OK, "增加成功");
    }

    /**
     * 修改
     *
     * @param comment
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody Comment comment, @PathVariable Long id) {
        comment.setId(id);
        commentService.update(comment);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable Long id) {
        commentService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @DeleteMapping("batchDelete")
    public Result batchDelete(@RequestBody List<Long> commentIds) {
        commentService.batchDelete(commentIds);
        return new Result(StatusCode.OK);
    }


    /**
     * 点赞/取消点赞
     *
     * @param comment
     * @return
     */
    @RequestMapping(value = "/thumb", method = RequestMethod.POST)
    public Result thump(@RequestBody Comment comment, @RequestAttribute(value = "user_id", required = false) String user_id, @RequestParam Long userId) {
        String message = null;
        if (!StringUtils.isEmpty(user_id)) {
            message = commentService.thump(comment, Long.valueOf(user_id));
        } else {
            message = commentService.thump(comment, userId);
        }
        return new Result(true, StatusCode.OK, message);
    }


    /**
     * 评论删除权限
     *
     * @param id
     * @param user_id
     * @return
     */
    @RequestMapping(value = "/enableToDelete/{id}", method = RequestMethod.GET)
    public Result enableToDelete(@PathVariable Long id, @RequestAttribute(value = "user_id", required = false) String user_id) {
        Boolean enableToDelete = commentService.enableToDelete(id, user_id);
        return new Result(true, StatusCode.OK, "删除权限", enableToDelete);
    }

    @GetMapping("/findCommentByTopCommentId/{page}/{size}")
    public Result findCommentByTopCommentId(@PathVariable Integer page, @PathVariable Integer size, @RequestParam("topCommentId") Long topCommentId, @RequestParam(required = false) Long userId) {

        return new Result(commentService.findCommentByTopCommentId(topCommentId, page, size, userId));
    }

//    @GetMapping("/getOneCommentByVideoId")
//    public Result getOneCommentByVideoId(@RequestParam Long videoId){
//        Comment = commentService.getOneCommentByVideoId(videoId);
//    }


}
